﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Utilities;

namespace NetTopologySuite.Noding
{
	public class SegmentPointComparator
	{
		public static int Compare(Octants octant, Coordinate p0, Coordinate p1)
		{
			if (p0.Equals2D(p1))
			{
				return 0;
			}
			int num = SegmentPointComparator.RelativeSign(p0.X, p1.X);
			int num2 = SegmentPointComparator.RelativeSign(p0.Y, p1.Y);
			switch (octant)
			{
			case Octants.Zero:
				return SegmentPointComparator.CompareValue(num, num2);
			case Octants.One:
				return SegmentPointComparator.CompareValue(num2, num);
			case Octants.Two:
				return SegmentPointComparator.CompareValue(num2, -num);
			case Octants.Three:
				return SegmentPointComparator.CompareValue(-num, num2);
			case Octants.Four:
				return SegmentPointComparator.CompareValue(-num, -num2);
			case Octants.Five:
				return SegmentPointComparator.CompareValue(-num2, -num);
			case Octants.Six:
				return SegmentPointComparator.CompareValue(-num2, num);
			case Octants.Seven:
				return SegmentPointComparator.CompareValue(num, -num2);
			default:
				Assert.ShouldNeverReachHere("invalid octant value: " + octant);
				return 0;
			}
		}

		public static int RelativeSign(double x0, double x1)
		{
			if (x0 < x1)
			{
				return -1;
			}
			if (x0 > x1)
			{
				return 1;
			}
			return 0;
		}

		private static int CompareValue(int compareSign0, int compareSign1)
		{
			if (compareSign0 < 0)
			{
				return -1;
			}
			if (compareSign0 > 0)
			{
				return 1;
			}
			if (compareSign1 < 0)
			{
				return -1;
			}
			if (compareSign1 > 0)
			{
				return 1;
			}
			return 0;
		}
	}
}
